﻿using System;
using UnityEngine;
using System.Collections;

public class Basics3 : MonoBehaviour {
	
	// Update is called once per frame
	void Update () {
		if(Network.isServer){
			 //Only the server can move the cube!
			 Vector3 moveDirection  = new Vector3(Input.GetAxis("Horizontal"),
				0f,Input.GetAxis("Vertical"));
			 float speed  = 30f;
			 transform.Translate(speed * moveDirection * Time.deltaTime);
		}
	}

	private void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
	{
		if (stream.isWriting)
		{
			Vector3 pos = transform.position;
			//"Encode" it, and send it
			stream.Serialize(ref pos);
		}
		else
		{
			Vector3 posReceive = Vector3.zero;
			//"Decode" it and receive it
			stream.Serialize(ref posReceive); 
			transform.position = posReceive;
		}
	}
}
